function test()

    clc 
    close all
    bands = 224
    %%loadAllData(bands)  %% Step1：把原始数据导出为mat文件
    
    %% 7月26日的数据
    if exist('F:\', 'dir') == 7
        src_root_path = 'F:\12份土壤\matlab';
    else
        src_root_path = 'D:\soil';
    end
        
    datapath = [src_root_path, '\data'];%% 反射率提取后的 Mat数据文件
    dataSrcInput = [src_root_path, '\input'];%% 原始光谱反射率TXT文件
    fileSavePath = [src_root_path, '\output'];%% R2，RMSE结果输出文件
%     %% 7月28日的数据
    datapath = [src_root_path, '\data\20220728'];%% 反射率提取后的 Mat数据文件
    dataSrcInput =  [src_root_path, '\input\20220728'];%% 原始光谱反射率TXT文件
    fileSavePath =  [src_root_path, '\output'];%% R2，RMSE结果输出文件
    
    
    args.bands = bands;
    args.removeHead = 0;%去掉前10    
    args.removeHead = 7;%去掉前10-5
    args.removeTail = 0;
    args.removeTail = 11;%%去掉后10-20
    args.containMetal = false;
    args.containMetal = true;%%保留金属
    
    args.dataSrcInput = dataSrcInput;%%Step 1    
    args.fileSavePath = fileSavePath;
    
    isBatch = true
    isBatch = false
    isBatch = true
    
    if isBatch == true
        DoLoadAllData(datapath, args)    %%进入批量处理操作
        return;
    end
   
    
   
    fileName = 'F:\12份土壤\hou\20220726_soil_2022-07-26_02-55-29-2.txt'
    
    roi =  MyRoi(bands);
    roi.loadOneFile(fileName)
    
    roi.points
    roi.regionNo
    roi.stats
    roi.hist
    
    
    stats = roi.stats;
%     xx = 1 : bands;
    errorbar(stats(:, 1), stats(:, 4), stats(:, 5));
    
%     
%     figure
%     Helper.plotTest()
    
%     Helper.plotRegion(stats(:, 1)', stats(:, 4)', stats(:, 5)')
% %     Helper.plotRegion(stats(:, 1), stats(:, 4), stats(:, 5))

% % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % 

end


function DoLoadAllData(datapath, args)
    step1 = 1;      step2 = 2;     step3 = 4;       step4 = 8;
    step5 = 16;     step6 = 32;    step7 = 128;     step8 = 256;
    step9 = 512;    step10 = 1024; step11 = 2048;
    step12 = step11 * 2;    step13 = step12 * 2;    step14 = step13 * 2;
    step15 = step14 * 2;    step16 = step15 * 2;    step17 = step16 * 2;
    step18 = step17 * 2;    step19 = step18 * 2;    step20 = step19 * 2;
    step21 = step20 * 2;    step22 = step21 * 2;    step23 = step22 * 2;
    step24 = step23 * 2;    step25 = step24 * 2;    step26 = step25 * 2;
    step27 = step26 * 2;    step28 = step27 * 2;    step29 = step28 * 2;
    step30 = step29 * 2;
    step = 0;
%     step = step + step1;%%Step1：把原始数据roi.txt导出为mat文件
    step = step + step2;%%Step2：加载数据，去掉前5后15
%     step = step + step3;%多个分量进行相关分析

    rng(20230214)
    bCVfold = false; %% 表示不做交叉验证
%     bCVfold = true;  %% 表示做交叉验证
    
    if bCVfold == true
        %         cc = cvpartition(24, 'KFold', 12);%%12折
        %     cc = Helper.GetMyCVParition();%%最新的12折 2023-08-08
        cc = cvpartition(24, 'KFold', 24);%%24折,h后面记得设置
    else
        cc.NumTestSets = 0; %%=0 表示不进行cv
    end

%     windowSize = 7; %% for MA
%     windowSize = 3; %% for 一阶导数

%     step = step + step4;%PLSRegresrsion    预测模型
%     step = step + step5;%%SVR
%     step = step + step6;%%随机森林
% %     step = step + step7;%%lasso 时间太慢了，不用,
%     step = step + step8; %% LSBoost 2023-08-09
   
    % step = step + step9;%%改CR,       预处理方法
%     step = step + step10;%%SG平滑
    step = step + step11;%%MSC
%     step = step + step12;%%SNV
%     step = step + step13;%%MA
%     step = step + step14;%%一阶导数 %先做三点平滑 2024-07-29
%     step = step + step15;%%二阶导数


%     step = step + step16;%%SPA    特征选择
%     step = step + step17;%%fsrftest 
%     step = step + step18;%fsrnca
%     step = step + step19;%sequentialfs %%用作测试
%     step = step + step20;%relieff
%      step = step + step21%cars 竞争自适应重加权采样 competitive adaptive reweighted sampling
%      step = step + step22%Random Frog
%       step = step + step23%LAR不行，改iriv， iriv运行太慢，不用了
%     step = step + step24%随机森林选择
% 	step = step + step25%UVE    
% %     
%      step = step + step26%特征选择 原step24
%      step = step + step27%特征选择结果保存
%       step = step + step28%显示选取波长
%      step = step + step29%% 多个ret文件合并
%       step = step + step30%% 单个元素的同一建模方法与所有预处理

     bands = args.bands;
     fileSavePath = args.fileSavePath;
     fileMatPath = datapath
     if bitand(step, step1)     
        disp('Step 1. ...............');
        isRefl = true
        Helper.loadAllData(args.dataSrcInput, bands, isRefl, fileMatPath)
        
     end
     if bitand(step, step2)     
        disp('Step 2. ...............');
% %         loadAllData(bands)
        [y_true, xx]  = loadExcelData(datapath); 
        corrcoef(y_true') %%y_true:11*12, xx:23*1cells
%         plotmatrix(y_true')
        
        if args.removeHead > 0 || args.removeTail > 0 
            xx = MyRoi.RemoveBands(xx, args.removeHead , args.removeTail);
        end
        if args.containMetal == true
            y_true = y_true(6:end, : );%%只留后面的金属
        end
% %         Helper.plot12Curve(xx, '12条光谱曲线');


        column = 4;%%取第四列平均光谱
        y_data = repmat(y_true, 1, 2);%%6*24
        x_data =  Helper.extractMatrix(xx, column);%%204*24
        %%后面都要改      
        Helper.plotPair(x_data);%%x_data:206*24
     end
     
     
     if bitand(step, step3)     
        disp('Step 3. ...............');
        
        Helper.showCorrelationEachBand(x_data, y_data);
        
     end
     % % % % % % % 预处理 % % % % % % % % % % % % % % % % % % % % % % % % % % %
     % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % %
     % % % 注意预处理x_data与y_data不用转置，但模型需要转置
     %%%% conitnumm removal
     if bitand(step, step9)     
        disp('Step 9. Conitnumm Removal...............');
        bShow = false;        
        bShow = true;  
        x_data = PreHelper.DoCR(x_data, bShow);%%x_data, 204*24
     end
     
     %%%%%%%进行SG滤波  
     if bitand(step, step10)     
        disp('Step 10. SG滤波...............');

        x_data = PreHelper.DoSG(x_data);%%x_data, 204*24
     end
     %%%%%%%进行MSC预处理  
     if bitand(step, step11)     
        disp('Step 11. MSC预处理...............');
        x_data = PreHelper.DoMSC(x_data);%%x_data, 204*24
        
        figure
        plot(x_data)
     end
     
     %%%%%%%进行SNV预处理  
     if bitand(step, step12)     
        disp('Step 12. SNV预处理...............');
        x_data = PreHelper.DoSNV(x_data);%%x_data, 204*24 
     end
     
     %%%%%%%进行MA滤波  
     if bitand(step, step13)     
        disp('Step 13. MA滤波...............');
        x_data = PreHelper.DoMA(x_data, windowSize);%%x_data, 204*24
     end 
     
     %%%%%%%进行1阶导数滤波  
     if bitand(step, step14)     
        disp('Step 14. 1阶导数滤波...............');
        bShow = true;
        bShow = false;
%         x_data = PreHelper.DoMA(x_data, 3);%先做三点平滑
        x_data = PreHelper.DoDiff(x_data, 1, bShow);%%x_data, 204*24           
     end 
     
     %%%%%%%进行2阶导数滤波  
     if bitand(step, step15)     
        disp('Step 15. 2阶导数滤波...............');
        bShow = true
%         bShow = false

        x_data = PreHelper.DoDiff(x_data, 2, bShow);            
     end 
     
     
     % % % % % 特征/波段选择 % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % %
     % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % %
      %%%%%%%进行SPA预处理  
     if bitand(step, step16)     
        disp('Step 16. SPA处理...............');
        bShow = false;
        bShow = true;
        ncomp = 10
        ins = 1:6;
%         kopt = FeaSelect.DoSPA(x_data', y_data', 23, ncomp, bShow);     
        kopt = FeaSelect.DoSPAEx(x_data', y_data', 20, ncomp, ins);   

        if (Helper.isModelling(step) == false)
            ncomp = 10
            bShow = true
%             ret = MyModel.DoEachMLSRegression(x_data', y_data', ncomp, kopt, bShow);
            ret = MyModel.DoEachMLSRegressionGroup(x_data', y_data', kopt, bShow, ncomp)
            fileSaveName = fullfile(fileSavePath, 'SPA-MLS-each.txt');
            Helper.SaveStatsToFiles(fileSaveName, ret);
        end
     end
     
     %%%%%%%进行fsrftest滤波   Univariate Feature Ranking Using F-Tests
     if bitand(step, step17)     
        disp('Step 17. fsrftest...............')
        bShow = false
        ncomp = 10
        fsss = FeaSelect.DoFsrfTest(x_data', y_data', ncomp, bShow);%%没有
       
        bShow = true
        ncomp = 10        
        ret = MyModel.DoEachMLSRegressionGroup(x_data', y_data', fsss, bShow, ncomp)
        fileSaveName = fullfile(fileSavePath, 'fsrftest-MLS-each.txt');
        Helper.SaveStatsToFiles(fileSaveName, ret);
     end
     
     %%%%%%%进行fsrnca滤波  
     if bitand(step, step18)     
        disp('Step 18. fsrnca...............')
        %%效果不太好，结果不直观，有些comp只有1个，不用
        bShow = true;
        bShow = false;
        [ret, mdls] = FeaSelect.DoFsrNCA(x_data', y_data', bShow);%%
        fileSaveName = fullfile(fileSavePath, 'NCA-MLS-each.txt');
        Helper.SaveStatsToFiles(fileSaveName, ret);      
        
% %         kopt = [1,88, 127, 199, 201];
% %         bShow = false;
% %         ncomp = 5;
% %         ret = Helper.DoEachMLSRegression(x_data', y_data', ncomp, kopt, bShow);
% %         fileSaveName = fullfile(fileSavePath, 'test-MLS-fsrnca.txt');
% %         Helper.SaveStatsToFiles(fileSaveName, ret);
     end
     
     
     %%%%%%%进行sequentialfs选择 
     if bitand(step, step19)     
        disp('Step 19. sequentialfs选择...............');
        %% 特征机制需要y，那就不能用
        %% 但部分元素只筛选出一个波段不能用，且里面用到交叉验证与最小二乘loss

        bShow = false
        ncomp = 10;
        fsss = FeaSelect.DoCarsNew(x_data', y_data', bShow, ncomp);
%         fsss = FeaSelect.DoiRF(x_data', y_data', bShow, ncomp);
        
% %         fsss = FeaSelect.DoSFS(x_data', y_data');
% %         bShow = true
% %         ncomp = 10;
        ret = MyModel.DoEachMLSRegressionGroup(x_data', y_data', fsss, bShow)
%         fileSaveName = fullfile(fileSavePath, 'carsnew-MLS-each.txt');
        fileSaveName = fullfile(fileSavePath, 'irf-MLS-each.txt');
        Helper.SaveStatsToFiles(fileSaveName, ret);
     end
     
      %%%%%%%进行relieff选择 
     if bitand(step, step20)     
        disp('Step 20. relieff 选择...............');

        bShow = false
        ncomp = 10;
        fsss = FeaSelect.DoRelieff(x_data', y_data', bShow, ncomp);
        bShow = true
        
        ret = MyModel.DoEachMLSRegressionGroup(x_data', y_data', fsss, bShow, ncomp)
        fileSaveName = fullfile(fileSavePath, 'relieff-MLS-each.txt');
        Helper.SaveStatsToFiles(fileSaveName, ret);
     end
     
     %%%%%%%进行cars选择 
     if bitand(step, step21)     
        disp('Step 21. cars 选择...............');

        bShow = false
        ncomp = 10;
        fsss = FeaSelect.DoCars(x_data', y_data', bShow, ncomp);
        bShow = true;
        ret = MyModel.DoEachMLSRegressionGroup(x_data', y_data', fsss, bShow, ncomp)
        fileSaveName = fullfile(fileSavePath, 'cars-MLS-.txt');
        Helper.SaveStatsToFiles(fileSaveName, ret);
     end    
     
     %%%%%%%进行随机蛙跳选择 
     if bitand(step, step22)     
         %%不采用了，选择的波段比较集中
         %% 出来波段结果：182   183   146   181   184   174   179   186   173   185
        disp('Step 22. 随机蛙跳选择...............');
        addpath('F:\12份土壤\matlab\iRF')  
        
        bShow = false
        bShow = true
        ncomp = 10;
        fsss = FeaSelect.DoRandomFrog(x_data', y_data', bShow, ncomp);
        bShow = true;
        ret = MyModel.DoEachMLSRegressionGroup(x_data', y_data', fsss, bShow, ncomp)
        fileSaveName = fullfile(fileSavePath, 'frog-MLS-.txt');
        Helper.SaveStatsToFiles(fileSaveName, ret);
     end
% % % % % % % % % % % % % % % % % % % % % % % % %      
     if bitand(step, step23)
        disp('Step 23. 改为IRIV   LAR选择...............');
%         addpath('F:\12份土壤\matlab\lar')  
%         addpath('F:\12份土壤\matlab\lars_lasso\lars')   
        addpath('libPLS_1.98')   
        bShow = false
        ncomp = 10;
        
        yy = y_data';
% %         F = iriv(x_data', yy(:, 4), 10, 6)        
% %         save('iriv.mat', 'F');
        dataI = load('iriv.mat');
        F = dataI.F
        
%         fsss = FeaSelect.DoLAR(x_data', y_data', bShow, ncomp);
        bShow = true;
        xx = x_data';
        fsss =cell(6,1);
        fsss{1} = F.SelectedVariables;
        fsss{2} = F.SelectedVariables;
        fsss{3} = F.SelectedVariables;
        fsss{4} = F.SelectedVariables;
        fsss{5} = F.SelectedVariables;
        fsss{6} = F.SelectedVariables;
% %         xx = xx(:, F.SelectedVariables);
        ret = MyModel.DoEachMLSRegressionGroup(x_data', y_data', fsss, bShow, ncomp)
        fileSaveName = fullfile(fileSavePath, 'irir-MLS-.txt');
        Helper.SaveStatsToFiles(fileSaveName, ret);
     end    
     
 % % % % % % % % % % % % % % % % % % % % % % % % %      
     if bitand(step, step24)
        disp('Step 24. 随机森林选择...............');
        %%% 不采用了，和建模方法一样的话 
        bShow = false
        ncomp = 10;
        fsss = FeaSelect.DoFeaRFTree(x_data', y_data', bShow, ncomp);
        bShow = true;
        ret = MyModel.DoEachMLSRegressionGroup(x_data', y_data', fsss, bShow, ncomp)
        fileSaveName = fullfile(fileSavePath, 'tree-MLS-each.txt');
        Helper.SaveStatsToFiles(fileSaveName, ret);
     end    
   % % % % % % % % % % % % % % % % % % % % % % % % %      
     if bitand(step, step25)
        disp('Step 25. UVE 选择...............');
        
        
        bShow = false
%         bShow = true
        ncomp = 10;
        fsss = FeaSelect.DoUVE(x_data', y_data', bShow, ncomp);
        Helper.DrawSelectionOnMeanSpectral(x_data', fsss)
% % % % %         yyy = y_data';
% % % % %         fsss = mcuvepls(x_data', yyy(:, 4), 10);
        bShow = true;
        
        ret = MyModel.DoEachMLSRegressionGroup(x_data', y_data', fsss, bShow, ncomp)
        fileSaveName = fullfile(fileSavePath, 'UVE-MLS-each.txt');
        Helper.SaveStatsToFiles(fileSaveName, ret);
     end           
     % % % % % % % % % % % % % % %      % % % % % % % % % % % % % % % % % % % % % % %
     % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % %
     % % % % % 回归模型 % % % % % % % % % % % % % % % % % % % % % % % % % % %
     % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % %
     %%%%%%偏最小二乘回归
     if bitand(step, step4)     
        disp('Step 4.偏最小二乘回归 ...............');
% % % %         column = 4;
% % % %         y_data = repmat(y_true, 1, 2);
% % % %         x_data =  Helper.extractMatrix(xx, column);
        arg.bShow = true;
        arg.ncomp = 7; %%7;%%回归系数，不是波段数
% %         arg.ncomp = 10;%%这里用混
% %         arg.nn = 23;%%没用了

% % %         2024-07-13 注释下面代码
        arg.cv = cc.NumTestSets; %%12，默认24折
        arg.cc = cc;
% % % % % %         arg.cv = 0; %% =0 表示不进行cv 
        
        X = x_data';
        y = y_data';
        %%OK：显示23个分量的
% % % %         Helper.PlotPLSRComps(x_data', y_data', nn);      
        [ret, beta,~, MSE] = MyModel.DoPLSR(X, y, arg);     
        mse = MSE(2,1+arg.ncomp)%%只针对 cv取值    ，最后一个数 
        
% % % %         y_hat = [ones(size(X,1),1) X] * beta;
% % % %         ret = Helper.CalcAllErrors(y, y_hat, arg.bShow);
        fileSaveName = fullfile(fileSavePath, 'PLS-all.txt');
        Helper.SaveStatsToFiles(fileSaveName, ret);
        
        fileSaveName = fullfile(fileSavePath, 'yfit-PLS-all.txt');            
        Helper.SaveValuesToFile(fileSaveName, ret);
        
        if arg.cv > 1
            fileSaveName = fullfile(fileSavePath, 'mse-cv-PLS-all.txt');
            save(fileSaveName, '-ascii', '-tabs', 'mse');
        end
        
% % %         return;
        
        bEach = false;        
%         bEach = true;%% 显示逐个因变量的 PLS情况
        arg.bEach = bEach;
        [ret, mse] = MyModel.DoEachRegression(X, y,arg);
        
        fileSaveName = fullfile(fileSavePath, 'PLS-each.txt');
        Helper.SaveStatsToFiles(fileSaveName, ret);
        
        fileSaveName = fullfile(fileSavePath, 'yfit-PLS-each.txt');            
        Helper.SaveValuesToFile(fileSaveName, ret);
        
        if arg.cv >1
            fileSaveName = fullfile(fileSavePath, 'mse-cv-PLS-each.txt');
            save(fileSaveName, '-ascii', '-tabs', 'mse');
        end
        
% %         Helper.plotPredict(y, y_hat);%%画出预测值与测量值
        return;
% %         zX = X - mean(X);
% %         zY = y - mean(y);
        [zX, muX, sigmaX] = zscore(X);%%正规化竟然效果不好
        [zY, muY, sigmaY] = zscore(y);
% %         muY = mean(y);
% %         sigmaY = 1;
        
        Helper.PlotPLSRComps(zX, zY, nn);
        beta = MyModel.DoPLSR(zX, zY, ncomp, bShow);
        
        aa = [ones(size(zX,1),1) zX] * beta;
        y_hat = sigmaY .* aa + muY;
        
        Helper.PlotStemErrors(y, y_hat)
        ret = Helper.CalcAllErrors(y, y_hat, bShow);
        
        
     end
     
     %%支持向量回归
     if bitand(step, step5)     
        disp('Step 5. 支持向量回归...............');
% % % %         column = 4;
% % % %         y_data = repmat(y_true, 1, 2);
% % % %         x_data =  Helper.extractMatrix(xx, column);
        bShow = true;
        
        X = x_data';
        y = y_data';
%         X = X - mean(X, 1);
%         y = y - mean(y, 1);

%         type = 5;
%         type = 4;
%         type = 3;
%         type = 2;
%         type = 1;
%         ret = MyModel.DoEachSVR(X, y, bShow, type);
%         fileSaveName = fullfile(fileSavePath, ['SVR-', num2str(type), '.txt']);
%         Helper.SaveStatsToFiles(fileSaveName, ret);
        rng(20230214)

        kfold = cc.NumTestSets; %%12;%% 8
        %       kfold = 1;%%默认不交叉
        for type = 2 :4  %type=1,5不要了太差了

            [ret, yfit, mse] = MyModel.DoEachSVR(X, y, bShow, type, kfold, cc);
            fileSaveName = fullfile(fileSavePath, ['SVR-', num2str(type), '.txt']);
            Helper.SaveStatsToFiles(fileSaveName, ret);
            
            fileSaveName = fullfile(fileSavePath, ['yfit-SVR-', num2str(type), '.txt']);            
            Helper.SaveValuesToFile(fileSaveName, ret);
            
            if kfold > 1
                disp(mse)
                fileSaveName = fullfile(fileSavePath, ['mse-cv-SVR-', num2str(type), '.txt']);
                save(fileSaveName, '-ascii', '-tabs', 'mse'); 
            end      
%             Helper.plotPredict(y, yfit)
        end
        
     end
     %% 决策树 ，随机森林
     if bitand(step, step6)     
        disp('Step 6. 随机森林 ...............');
% %         column = 4;
% %         y_data = repmat(y_true, 1, 2);
% %         x_data =  Helper.extractMatrix(xx, column);
        bShow = false;
        
        X = x_data';
        y = y_data';  
        
       
        rng(20221206)
        kfold = cc.NumTestSets;
% % %         kfold = 0;  %%记得要在此设置


% % % % %          %%找出最优分量
% % % % %         ret = MyModel.DoEachRFwithBest(X, y, bShow);
% % % % %         fileSaveName = fullfile(fileSavePath, ['best_tree-1.txt']);
% % % % %         Helper.SaveStatsToFiles(fileSaveName, ret);      
% % % % %         return
        
% %         这个不需要了，也没啥用
% % % %         ret = MyModel.DoEachRFwithBest2(X, y, bShow);
% % % %         fileSaveName = fullfile(fileSavePath, ['best_tree-2.txt']);
% % % %         Helper.SaveStatsToFiles(fileSaveName, ret);
%         return
        %%决策树
% % % % %         ret = Helper.DoEachTree(X, y, bShow);
% % % % %         fileSaveName = fullfile(fileSavePath, ['fitrtree.txt']);
% % % % %         Helper.SaveStatsToFiles(fileSaveName, ret);
% % % % % % % %         return
        
        %%随机森林
        ret = MyModel.DoEachRF(X, y, bShow, kfold, cc);
      
        fileSaveName = fullfile(fileSavePath, ['random_tree.txt']);
        Helper.SaveStatsToFiles(fileSaveName, ret);        
        
        fileSaveName = fullfile(fileSavePath, 'yfit-random_tree.txt');            
        Helper.SaveValuesToFile(fileSaveName, ret);
     end
     %% LSBoost
     if bitand(step, step8)     
        disp('Step 8. LSBoost ...............');
        bShow = true;        
        X = x_data';
        y = y_data';  
               
        rng(20221206)
        kfold = cc.NumTestSets;
%         kfold = 0;

        ret = MyModel.DoEachLSB(X, y, bShow, kfold, cc);
      
        fileSaveName = fullfile(fileSavePath, ['LSBoost.txt']);
        Helper.SaveStatsToFiles(fileSaveName, ret);       
        
        fileSaveName = fullfile(fileSavePath, 'yfit-LSBoost.txt');            
        Helper.SaveValuesToFile(fileSaveName, ret);
        
     end
%      lasso太慢，没用
%      if bitand(step, step7)     
%         disp('Step 7. Lasso...............');
% 
%         bShow = true;        
%         X = x_data';
%         y = y_data';
%        
%         ret = Helper.DoEachLasso(X, y, bShow);
%         fileSaveName = fullfile(fileSavePath, 'lasso.txt');
%         Helper.SaveStatsToFiles(fileSaveName, ret);       
%      end

% % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % 
% % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % 
% % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % %
% % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % %
% % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % %
% %     methods = zeros(6, 2);
%     methods(1,1) = 2;    methods(1,2) = 4;%%%2024-07-29 之前的
% % %     methods(2,1) = 1;    methods(2,2) = 1;%PLSR 采用了7个
% % %     methods(3,1) = 1;    methods(3,2) = 3;
% % %     methods(4,1) = 2;    methods(4,2) = 3;
% % %     methods(5,1) = 3;    methods(5,2) = 4;
% % %     methods(6,1) = 1;    methods(6,2) = 3;
    %% preType         modelType
% %     methods(1,1) = 1;    methods(1,2) = 1; %PLSR 采用了10个
% %     methods(2,1) = 1;    methods(2,2) = 1;
% %     methods(3,1) = 1;    methods(3,2) = 3;
% %     methods(4,1) = 2;    methods(4,2) = 4;
% %     methods(5,1) = 3;    methods(5,2) = 3;
% %     methods(6,1) = 1;    methods(6,2) = 3;
    
    methods(1,1) = 2;    methods(1,2) = 4;   %PLSR 采用了8个
    methods(2,1) = 1;    methods(2,2) = 1;
    methods(3,1) = 1;    methods(3,2) = 3;
    methods(4,1) = 2;    methods(4,2) = 3;
    methods(5,1) = 2;    methods(5,2) = 3;
    methods(6,1) = 1;    methods(6,2) = 3;
    
    
    if bitand(step, step26)     
        disp('Step 26. 特征选择...............');

        bShow = true;        
        bShow = false;
% %         X = x_data';%%预处理不需要转置
% %         y = y_data';
       
        arg.bShow = bShow;
        arg.ncomp = 8;   %%PLSR的回归系数
        arg.cv = cc.NumTestSets; %%12，默认24折
        arg.cc = cc;        
      
        arg.methods = methods;   
        arg.feaTypes = 6%%6%%4 %%%5  %%特征选择方法的个数
        
        ret = Helper.DoAllMetalsModelFeaturePreProcess(x_data, y_data, arg)
        
        fileSaveName = fullfile(fileSavePath, 'All-metals-features.mat');
        save(fileSaveName, 'ret');
    end
    
    
    if bitand(step, step27)
        disp('Step 27. 特征选择结果保存...............');
        fileSaveName = fullfile(fileSavePath, 'All-metals-features.mat');
        ret = load(fileSaveName, 'ret');
        ret = ret.ret;
        data = Helper.SetResultForOneMetal(ret)

        
        fileSave = cell(6, 1);
        fileSave{1} =  'fea-metal-1.txt';
        fileSave{2} =  'fea-metal-2.txt';
        fileSave{3} =  'fea-metal-3.txt';
        fileSave{4} =  'fea-metal-4.txt';
        fileSave{5} =  'fea-metal-5.txt';
        fileSave{6} =  'fea-metal-6.txt';
        for i = 1 : 6
            fileSave{i} = fullfile(fileSavePath, fileSave{i});
        end
        arg.fileSave = fileSave;
%          fileSave = arg.fileSave;
         for metal = 1 : size(methods, 1)
             rrr = data{metal};
             if isempty(rrr)
                 continue
             end                 
             
             tmp = [rrr.rmse; rrr.R2; rrr.rsd; rrr.rpd; rrr.mape];
             save(fileSave{metal}, '-ascii', '-tabs', 'tmp');
         end        
    end
    
    if bitand(step, step28)
        disp('Step 28. 列出特征选择的 波段...............');
       
% %         setting = [2, 4, 4;
% %                    1, 1, 1;
% %                    1, 3, 1;
% %                    2, 3, 3;
% %                    3, 4, 1;
% %                    1, 3, 1];
        setting = [2, 4, 4;
                   1, 1, 1;
                   1, 3, 1;
                   2, 3, 3;
                   2, 3, 1;
                   1, 3, 1];       
        
        fileSave = cell(6, 1);
        fileSave{1} =  'waves-opt-metal-1.txt';
        fileSave{2} =  'waves-opt-metal-2.txt';
        fileSave{3} =  'waves-opt-metal-3.txt';
        fileSave{4} =  'waves-opt-metal-4.txt';
        fileSave{5} =  'waves-opt-metal-5.txt';
        fileSave{6} =  'waves-opt-metal-6.txt';
        for i = 1 : 6
            fileSave{i} = fullfile(fileSavePath, fileSave{i});
        end
        arg.fileSave = fileSave;
        arg.bShow = false;
        arg.wave = 10;
%          fileSave = arg.fileSave;
        kopt = zeros(6, arg.wave);
        for metal = 1 : size(setting, 1)
            preType   = setting(metal, 1);
            feaType   = setting(metal ,3);             
            
            ret = Helper.DoOneMetalWithOneSetting(x_data, y_data, metal, preType, feaType, arg); 
            kopt(metal, :) = ret;
%             save(fileSave{metal}, '-ascii', '-tabs', 'ret');            

            fsss{metal} = ret;
        end         
        
        FeaSelect.plotBands(x_data', y_data', fsss, 10);
        FeaSelect.plotBandsEachMetal(x_data', y_data', fsss, 10, setting);
        
        fileSave = fullfile(fileSavePath, 'waves-opt-metals-all.txt');
        save(fileSave, '-ascii', '-tabs', 'kopt');
    end
    
% % % % % % % % % % % % % % % % % % % % % % % % % %     
    if bitand(step, step29)
        disp('Step 29. 合并ret文件...............');
       
        file1 = fullfile(fileSavePath, 'All-metals-features1.mat');
        file2 = fullfile(fileSavePath, 'All-metals-features2.mat');
        fileSaveName = fullfile(fileSavePath, 'All-metals-features.mat');
        
        nCut = 5
        ret = Helper.MergeRetFiles(file1, file2, fileSaveName, nCut)
    end
    
% % % % % % % % % % % % % % % % % % % % % % % % % %     
    if bitand(step, step30)
        disp('Step 30. 一个模型，多个元素多种预处理...............');
        
        bShow = false;
       
        arg.bShow = bShow;
        arg.ncomp = 7;   %%PLSR的回归系数
        arg.cv = cc.NumTestSets; %%12，默认24折
        arg.cc = cc;        
        arg.preTypes = 4;
        arg.wait = 8;
        
        modelType = 1
        modelType = 2
        modelType = 3
        modelType = 4

        ret = Helper.DoOneModelWithAllSetting(x_data, y_data, modelType, arg)
        fileSave = fullfile(fileSavePath, ['all-preTypes-one-model-', num2str(modelType), '.mat']);
        save(fileSave, 'ret');
        
% %         ret = load(fileSave, 'ret');
% %         ret = ret.ret;
        
        data = [];
        for preType = 1 : arg.preTypes 
             rrr = ret{preType};     
             
             tmp = [rrr.rmse; rrr.R2; rrr.rsd; rrr.rpd; rrr.mape];
             zzz = zeros(size(tmp, 1), 1);
             
             if preType == 1
                 data = tmp;
             else
                data = [data, zzz, tmp];
             end
        end        
        
        fileSave = fullfile(fileSavePath, ['all-preTypes-one-model-', num2str(modelType), '.txt']);
        save(fileSave, '-ascii', '-tabs', 'data');
    end
     disp('All done');
     datetime
end


% % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % 
% % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % 
% % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % 
% % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % 
% % % % % % % % % % % % % % % % % % % % % % % % % % % % % % 
% % Step2:  加载化学检测数据，加载mat波谱数据
function [y_true, xx] = loadExcelData(datapath)
    fileName = 'data.txt';
    fileFullName = fullfile(datapath, fileName);
    
    fid = fopen(fileFullName);
    lines = textscan(fid,'%s %s %s %f %s %s', 'Delimiter','\t');
    data = lines{4};
    len = length(data);
    y_true = reshape(data, len/12,12);   
    fclose(fid)
    
    %%% 加载mat文件
    imdjson = imageDatastore(datapath, 'FileExtensions',{'.mat'}, 'IncludeSubfolders', false);
    xx = cell(length(imdjson.Files), 1);
    for i = 1 : length(imdjson.Files)  
        fileName = imdjson.Files{i};
        
        [~, fName, ~] = fileparts(fileName);
        lines = textscan(fName, '%d_%d');
        data = load(fileName);
        roi = data.roi;
        stats = roi.stats;
        
        first = lines{1};
        second = lines{2};
%         kk = (first - 1) * 2 + 1 + second - 1;
        kk = (second - 1) * 12 + 1 + first - 1;
        xx{kk} = stats;
    end
end


